package com.kkang.ch3.aware;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 调用aware示例
 *
 * @author yongk_liu
 **/
@Service
public class DemoAwareService implements BeanNameAware, ResourceLoaderAware {//实现这两个接口

    private String beanName;//将spring内容的bean自己存起来
    private ResourceLoader loader;//将spring的resourceloader存在自己的属性中

    @Override
    public void setBeanName(String name) {
        this.beanName = name;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader = resourceLoader;
    }

    public void outputResult() throws IOException {
        System.out.println("Bean的名称为：" + beanName);
        Resource resource = loader.getResource("classpath:com/kkang/ch3/aware/test.txt");
        System.out.println("Resource加载的文件内容为：" + IOUtils.toString(resource.getInputStream()));
    }

}
